/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.actions;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import de.renier.jkeepass.JKeePass;
import de.renier.jkeepass.Messages;
import de.renier.jkeepass.dialog.BasicElementDialog;
import de.renier.jkeepass.elements.BasicElement;
import de.renier.jkeepass.elements.ContainerElement;


/**
 * EditBasicElementAction
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class EditBasicElementAction extends AbstractAction {
  
  private static final long serialVersionUID = -5539198558494486987L;
  private BasicElement basicElement;
  
  public EditBasicElementAction() {
    super(Messages.getString("EditBasicElementAction.0"),new ImageIcon(EditBasicElementAction.class.getResource("/org/javalobby/icons/20x20/DocumentDraw.gif"))); //$NON-NLS-1$ //$NON-NLS-2$
  }
  
  /**
   * Constructor for EditBasicElementAction
   *
   * @param basicElement
   */
  public EditBasicElementAction(BasicElement basicElement) {
    this();
    this.basicElement = basicElement; 
  }
  
  /* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent e) {
    // only if we have an element to edit
    if (basicElement != null) {
      // input dialog for container
      BasicElementDialog containerDialog = new BasicElementDialog(JKeePass.application);
      containerDialog.setBasicElement(basicElement);
      containerDialog.show();
      
      // do we save it
      if (BasicElementDialog.RESULT_SAVE == containerDialog.getResult()) {
        // get the element to set values back to object
        containerDialog.getBasicElement();
        // get container
        TreePath treePath = JKeePass.application.getElementTreePanel().getLeadSelectionPath();
        if (treePath != null) {
          DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getLastPathComponent();
          if (node != null) {
            ContainerElement currentContainer = (ContainerElement)node.getUserObject();
            // refresh
            JKeePass.application.getContainerElementPanel().update(currentContainer);
            JKeePass.application.setModified(true);
          }
        }
      }
    }
  }

  /**
   * getBasicElement
   *
   * @return Returns the basicElement.
   */
  public BasicElement getBasicElement() {
    return basicElement;
  }

  /**
   * setBasicElement
   *
   * @param basicElement The basicElement to set.
   */
  public void setBasicElement(BasicElement basicElement) {
    this.basicElement = basicElement;
  }

}
